package loadiconsvc

import (
	"fmt"
	"gmap-backend/internal/config"
	"gmap-backend/internal/server/serviceimpl/loadiconsvc/loadiconhandler"
	"gmap-backend/internal/server/vo"
)

var fileLoadIconHandler = &loadiconhandler.FileLoadIcon{}

var subHandlers []loadiconhandler.LoadIconSubHandler = []loadiconhandler.LoadIconSubHandler{
	fileLoadIconHandler,
	&loadiconhandler.HttpLoadIcon{},
	&loadiconhandler.DirLoadIcon{},
	&loadiconhandler.CpPathLoadIcon{},
	&loadiconhandler.CmdOpenPathLoadIcon{},
	&loadiconhandler.OpenbyLoadIcon{},
	&loadiconhandler.OpenAsLoadIcon{},
	&loadiconhandler.OpenInLoadIcon{},
	&loadiconhandler.DirOpenByLoadIcon{},
	&loadiconhandler.GmapLoadIcon{},
}

// LoadIcon 加载指定url和图标
func LoadIcon(appConfig *config.AppConfig, req *vo.PathReq, bins [][]byte) *vo.LoadIconResp {
	// 尝试用每个处理器进行处理
	for _, handler := range subHandlers {
		if ok, extra := handler.CanHandle(appConfig, req); ok {
			return handler.Handle(appConfig, req, extra)
		}
	}

	// 如果没有找到匹配的处理器，则把地址包装为file:///，然后用文件处理器处理
	newReq := &vo.PathReq{
		Path:   "file:///" + req.Path,
		CtxDir: req.CtxDir,
	}
	newReq.Action = req.Action
	if ok, extra := fileLoadIconHandler.CanHandle(appConfig, newReq); ok {
		return fileLoadIconHandler.Handle(appConfig, newReq, extra)
	}
	panic(fmt.Sprintf("未知的url：\r\n%s", req.Path))
}
